package com.hushao.lims.weblims.controller;

import com.hushao.lims.weblims.domain.EquipmentStorage;
import com.hushao.lims.weblims.domain.TeacherInfo;
import com.hushao.lims.weblims.service.EquipmentStorageService;
import com.hushao.lims.weblims.vo.ResponseData;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 930588706
 * @date 18/1/22
 */
@RequestMapping("/equipmentStorage")
@Controller
public class EquipmentStorageController {

    @Autowired
    private EquipmentStorageService equipmentStorageService;
    @Autowired
    private UserDetailsService userDetailsService;


    /**
     * 器材入库查询
     * @param equipmentName
     * @param async
     * @param pageable
     * @param model
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "器材入库列表（分页）")
    @PreAuthorize("hasAuthority('ROLE_主管')")
    @GetMapping
    public String listEquipmentStorage(@RequestParam(value = "equipmentName",required = false,defaultValue = "") String equipmentName,
                                       @RequestParam(value = "state",required = false,defaultValue = "")String state,
                                       @RequestParam(value = "async",required = false) boolean async,
                                       Pageable pageable,
                                       Model model)throws Exception{
        Page<EquipmentStorage> equipmentStoragePage=equipmentStorageService.listEquipmentStorageByEquipmentNameLikeAndStateLike(equipmentName,state,pageable);
        List<EquipmentStorage> equipmentStorages=equipmentStoragePage.getContent();
        model.addAttribute("page",equipmentStoragePage);
        model.addAttribute("equipmentStorages",equipmentStorages);
        return async==true?"/audit/equipmentStorages ::#mainContainerRepleace":"/audit/equipmentStorages";
    }

    /**
     * 根据器材名称模糊查询个人器材入库
     * @param equipmentName
     * @param async
     * @param pageable
     * @param model
     * @return
     */
    @ApiOperation(value = "查询个人器材入库列表")
    @GetMapping("{teacherNumber}")
    public String listEquipmentStorageByTeacher(@PathVariable("teacherNumber")String teacherNumber,
                                                @RequestParam(value = "equipmentName",required = false,defaultValue = "") String equipmentName,
                                                @RequestParam(value = "async",required = false) boolean async,
                                                Pageable pageable,
                                                Model model)throws Exception{
        TeacherInfo teacher= (TeacherInfo) userDetailsService.loadUserByUsername(teacherNumber);
        Page<EquipmentStorage> equipmentStoragePage=equipmentStorageService.listEquipmentStorageByTeacherAndEquipmentName(equipmentName,teacher,pageable);
        List<EquipmentStorage> equipmentStorages=equipmentStoragePage.getContent();
        model.addAttribute("page",equipmentStoragePage);
        model.addAttribute("equipmentStorages",equipmentStorages);
        return async==true?"/equipments/storages ::#mainContainerRepleace":"/equipments/storages";
    }

    /**
     * 撤销待审核的器材入库（删除）
     * @param equipmentStorageId
     * @param teacherNumber
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "撤销待审核的器材入库（个人）")
    @ResponseBody
    @DeleteMapping("/{teacherNumber}/revocation/{equipmentStorageId}")
    @PreAuthorize("authentication.name.equals(#teacherNumber)")
    public ResponseData delete(@PathVariable("equipmentStorageId") Long equipmentStorageId,
                               @PathVariable("teacherNumber") String teacherNumber)throws Exception{
        equipmentStorageService.remove(equipmentStorageId);
        return new ResponseData(true,"撤销成功！");
    }

    /**
     * 完成已通过的器材入库
     * @param equipmentStorageId
     * @param teacherNumber
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "完成已通过的器材入库服务")
    @ResponseBody
    @PostMapping("/{teacherNumber}/achieve/{equipmentStorageId}")
    @PreAuthorize("authentication.name.equals(#teacherNumber)")
    public ResponseData achieve(@PathVariable("equipmentStorageId") Long equipmentStorageId,
                                @PathVariable("teacherNumber") String teacherNumber)throws Exception{
        equipmentStorageService.achieve(equipmentStorageId);
        return new ResponseData(true,"器材入库成功！");
    }
}
